import pygame
import sys
import random

# 初始化pygame
pygame.init()

# 设置窗口尺寸
WIDTH, HEIGHT = 640, 480
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("简单的弹球游戏")

# 设置颜色
WHITE = (255, 255, 255)

# 设置球的初始位置和大小
ball_pos = [WIDTH // 2, HEIGHT // 2]
ball_size = 20
ball_speed = [random.choice([-4, 4]), random.choice([-4, 4])]

# 游戏主循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 移动球
    ball_pos[0] += ball_speed[0]
    ball_pos[1] += ball_speed[1]

    # 碰到边界反弹
    if ball_pos[0] <= 0 or ball_pos[0] >= WIDTH - ball_size:
        ball_speed[0] = -ball_speed[0]
    if ball_pos[1] <= 0:
        ball_speed[1] = -ball_speed[1]
    if ball_pos[1] >= HEIGHT - ball_size:
        ball_speed[1] = -ball_speed[1]

    # 填充背景色
    screen.fill(WHITE)

    # 绘制球
    pygame.draw.circle(screen, (0, 0, 255), ball_pos, ball_size // 2)

    # 更新屏幕显示
    pygame.display.flip()

    # 控制游戏循环的帧率
    pygame.time.Clock().tick(60)

pygame.quit()
sys.exit()